﻿Imports System.Data.OleDb

Public Class AddProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim SqlString As String = "select * from profiles where wlUsername = @f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()

            Dim dr As OleDbDataReader = cmd.ExecuteReader()

            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("Name")) Then txtNames.Text = dr("Name")
                If Not IsDBNull(dr("Age")) Then txtAge.Text = dr("Age")
                If Not IsDBNull(dr("Sex")) Then cblGender.SelectedValue = dr("Sex")
                If Not IsDBNull(dr("Picture")) Then myPicture.ImageUrl = dr("Picture")
                If IsDBNull(dr("Picture")) Then myPicture.ImageUrl = "images\blank.jpg"
                If Not IsDBNull(dr("Description")) Then txtDescription.Text = dr("Description")
                If Not IsDBNull(dr("Location")) Then txtLocation.SelectedValue = dr("Location")
                If Not IsDBNull(dr("Weight")) Then txtWeight.Text = dr("Weight")
                If Not IsDBNull(dr("Height")) Then txtHeight.Text = dr("Height")
                If Not IsDBNull(dr("targetWeight")) Then txtTargetWeight.Text = dr("targetWeight")
                If Not IsDBNull(dr("emailAddress")) Then txtEmail.Text = dr("emailAddress")
                If Not IsDBNull(dr("Website")) Then txtWebsite.Text = dr("Website")
                If Not IsDBNull(dr("bgColor")) Then ddlbgColor.SelectedValue = dr("bgColor")
            End If
        End If
    End Sub

    Protected Sub btnAddProfile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddProfile.Click

        Dim UserID As String
        Dim MemUser As MembershipUser
        MemUser = Membership.GetUser
        UserID = MemUser.ProviderUserKey.ToString

        Dim newFileName As String = ""
        Dim SqlString As String

        If fuPicture.HasFile Then
            Dim myGUID As New System.Guid
            myGUID = Guid.NewGuid
            newFileName = myGUID.ToString() + ".jpg"
            Dim picLocation = Request.MapPath("pictures") & "/" & newFileName
            fuPicture.SaveAs(picLocation)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

        If fuPicture.HasFile Then
            SqlString = "Update profiles set UserID=@f1,Name=@f2,Age=@f3,Sex=@f4,Picture=@f5,Description=@f6,Location=@f7,Weight=@f8,Height=@f9,targetWeight=@f10,emailAddress=@f11,Website=@f12, bgColor=@f13 where wlUsername = @f14"
        Else
            SqlString = "Update profiles set UserID=@f1,Name=@f2,Age=@f3,Sex=@f4,Description=@f6,Location=@f7,Weight=@f8,Height=@f9,targetWeight=@f10,emailAddress=@f11,Website=@f12, bgColor=@f13 where wlUsername = @f14"
        End If

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)

        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", UserID)
        cmd.Parameters.AddWithValue("@f2", txtNames.Text)
        cmd.Parameters.AddWithValue("@f3", txtAge.Text)
        cmd.Parameters.AddWithValue("@f4", cblGender.SelectedValue)
        If fuPicture.HasFile Then cmd.Parameters.AddWithValue("@f5", "pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f6", txtDescription.Text)
        cmd.Parameters.AddWithValue("@f7", txtLocation.Text)
        cmd.Parameters.AddWithValue("@f8", txtWeight.Text)
        cmd.Parameters.AddWithValue("@f9", txtHeight.Text)
        cmd.Parameters.AddWithValue("@f10", txtTargetWeight.Text)
        cmd.Parameters.AddWithValue("@f11", txtEmail.Text)
        cmd.Parameters.AddWithValue("@f12", txtWebsite.Text)
        cmd.Parameters.AddWithValue("@f13", ddlbgColor.SelectedValue)
        cmd.Parameters.AddWithValue("@f14", User.Identity.Name)

        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()

        Response.Redirect("profile.aspx?wlUN=" & User.Identity.Name)

    End Sub
End Class